import AbstractManager from './core/AbstractManager';
import { action, makeObservable, observable } from 'mobx';
import { DesignerMode, ProjectDataType } from '../DesignerType';
import canvasManager from '../header/items/canvas/CanvasManager';
import layerManager from './LayerManager';
import themeManager from '../header/items/theme/ThemeManager';
import bluePrintManager from '../blueprint/manager/BluePrintManager';
import FilterManager from './FilterManager';

/**
 * 整个设计器的所有数据初始化和数据聚合，统一通过该管理器进行分发和处理
 */
class DesignerManager extends AbstractManager<ProjectDataType> {
  constructor() {
    super();
    makeObservable(this, {
      loaded: observable,
      setLoaded: action,
    });
  }

  loaded: boolean = false;

  setLoaded = (loaded: boolean) => (this.loaded = loaded);

  destroy(): void {
    canvasManager.destroy();
    layerManager.destroy();
    themeManager.destroy();
    bluePrintManager.destroy();
  }

  getData(): ProjectDataType {
    return {
      canvasManager: canvasManager.getData(),
      themeManager: themeManager.getData()!,
      layerManager: layerManager.getData(),
      bluePrintManager: bluePrintManager.getData(),
      filterManager: FilterManager.getData(),
    };
  }

  init(data: ProjectDataType, mode: DesignerMode): void {
    data.canvasManager && canvasManager.init(data.canvasManager!);
    data.themeManager && themeManager.init(data.themeManager!);
    data.layerManager && layerManager.init(data.layerManager!);
    data.bluePrintManager && bluePrintManager.init(data.bluePrintManager!, mode);
    data.filterManager && FilterManager.init(data.filterManager!);
  }
}

const designerManager = new DesignerManager();
export default designerManager;
